package yxy.game.pm2.command.passbarrier;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.passbariercost;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.message.PassBarrierMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.passbarrier.PassbarrierPlayerModule;


/****************************************
 * @name CMD_PassBarrierChallengePurchase
 * @description 斩关夺隘主次数购买
 * @author 梁健彬
 * @since 2022-09-05
 ***************************************/
@MessageType(proto = Command.PassBarrierChallengePurchase, classtype = PassBarrierMessage.PassBarrierChallengePurchaseRequest.class)
public class CMD_PassBarrierChallengePurchase extends Command<PassBarrierMessage.PassBarrierChallengePurchaseRequest> {

    @Override
    public void execute(MessageSource messageSource, PassBarrierMessage.PassBarrierChallengePurchaseRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int times = request.getTimes();
        PassBarrierMessage.PassBarrierChallengePurchaseResponse.Builder response = PassBarrierMessage.PassBarrierChallengePurchaseResponse.newBuilder();
        try {
            PassbarrierPlayerModule module = PassbarrierPlayerModule.get(playerCode);
            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            if (times < 0) {
                send(messageSource, response, Result.DataError);
                return;
            }

            passbariercost data = DataCenter.getData(2, passbariercost.class);
            int challengeExtraBuyNum = module.getChallengePurchaseNum() + times;
            if (challengeExtraBuyNum > data.getLimit()) {
                send(messageSource, response, Result.PassBarrierChallengePurchaseLimitCantBuy);//购买次数达到上限
                return;
            }

            //todo 消耗
            Currency cost = Currency.create(data.getCosttype(), data.getCostid(), data.getCostnum() + challengeExtraBuyNum * data.getExtra());
            try {
                new CurrencyHandle().cost(playerCode, CCReason.斩关夺隘, cost);
            } catch (CurrencyNotEnoughException e) {
                Result result = new Result(Result.CostFail, e.getType().getNumber(), e.getDataId());// 消费不足
                send(messageSource, response, result);
                return;
            }

            module.setChallengePurchaseNum(challengeExtraBuyNum);
            module.set();


            int num = module.getChallengePurchaseNum() + 1;
            Currency nextCost = Currency.create(data.getCosttype(), data.getCostid(), data.getCostnum() + num * data.getExtra());
            PackageMessage.Item fightBuyCost = new CurrencyHandle().packItem(nextCost);//下次消耗

            //todo 剩余挑战次数和挑战次数购买的消耗返回
            response.setFightNumCost(fightBuyCost);
            response.setTimes(module.getLessChallengeNum() + module.getChallengePurchaseNum());

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, PassBarrierMessage.PassBarrierChallengePurchaseResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


}
